package utils;



import java.io.File;
import java.io.IOException;

import net.sf.ehcache.search.Query;
import net.sf.ehcache.search.Results;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.SimpleFSDirectory;
import org.apache.lucene.util.Version;
import org.jsoup.Jsoup;

public class LucensTest {
	public static File dataDir =new File("D:/lucens/data");
	public static void main(String[] args) throws IOException, ParseException {
		  IndexReader reader = DirectoryReader.open(FSDirectory.open(new File("D:/lucens/data")));
		  IndexSearcher searcher = new IndexSearcher(reader);
		  Analyzer an = new SmartChineseAnalyzer();
		  QueryParser parser = new QueryParser("jsoup", an);
		  Query  query =  (Query) parser.Query("jsoup");
		  Results rs = query.execute();
		 System.out.println(rs.toString());
	}
	
	public static String getPage() throws IOException {
		org.jsoup.nodes.Document doc;
			doc = Jsoup.connect("http://www.begincode.net").get();
			return  doc.body().html();
		
	}
	public static void initIndex()throws IOException{
		Directory dir = FSDirectory.open(new File("D:/lucens/data"));
		Document doc = new Document();
		System.out.println(doc);
		SmartChineseAnalyzer an = new SmartChineseAnalyzer();
		IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_4_10_3,an);
		iwc.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
		IndexWriter iw = new  IndexWriter(dir, iwc);
		Field field = new StringField("yang",getPage(),Field.Store.NO);
		
		doc.add(field);
		iw.addDocument(doc);
		iw.close();
	}
}